package com.dbkj.common.utils;

import com.jfinal.kit.PropKit;
import com.jfinal.log.Log;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisCluster;

import java.util.LinkedHashSet;
import java.util.Set;

/**
 * @Author: Max
 * @Date: 2019/11/14 13:43
 */

public class RedisUtils {

    public static Jedis jedis;
    public static JedisCluster cluster;

    static{
        if(PropKit.getBoolean("is_cluster")){
            String redisCluster = PropKit.get("redis_cluster");
            redisCluster = redisCluster.replaceAll(" ","");
            String[] arrs = redisCluster.split(",");
            Set<HostAndPort> nodes = new LinkedHashSet<>();
            for(int i=0;i<arrs.length;i++){
                String[] strs = arrs[i].split(":");
                nodes.add(new HostAndPort(strs[0],Integer.parseInt(strs[1])));
            }
            cluster = new JedisCluster(nodes);
        }else{
            jedis = new Jedis(PropKit.get("redis_addr"),PropKit.getInt("redis_port"),10000);
        }

        Log.getLog(RedisUtils.class).info("Jedis初始化完毕");
        System.out.println("****Jedis初始化完毕***");
    }

}
